package bat.iog.hytool.tomcat.startup;

import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public final class Bootstrap {
    
    //用在main中，作为锁
    private static final Object daemonLock = new Object();

    //在初始化完成后实例化
    private static volatile Bootstrap daemon = null;

    //通过反射得到的Catalina对象
    private Object catalinaDaemon = null;

    ClassLoader commonLoader = null;
    ClassLoader catalinaLoader = null;
    ClassLoader sharedLoader = null;
    
    public static void main(String[] args) {
        //加锁，线程安全
        synchronized (daemonLock) {
            if (daemon == null) {
                Bootstrap bootstrap = new Bootstrap();
                try {
                    //初始化类加载器
                    bootstrap.init();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                daemon = bootstrap;
            }
        }

        try {
            daemon.setAwait(true);
            daemon.load(args);
            daemon.start();
            if (null == daemon.getServer()) {
                System.exit(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
    public void setAwait(boolean await)
            throws Exception {

        Class<?> paramTypes[] = new Class[1];
        paramTypes[0] = Boolean.TYPE;
        Object paramValues[] = new Object[1];
        paramValues[0] = Boolean.valueOf(await);
        Method method = catalinaDaemon.getClass().getMethod("setAwait", paramTypes);
        method.invoke(catalinaDaemon, paramValues);
    }

    private void load(String[] arguments) throws Exception {

        // Call the load() method
        String methodName = "load";
        Object param[];
        Class<?> paramTypes[];
        if (arguments==null || arguments.length==0) {
            paramTypes = null;
            param = null;
        } else {
            paramTypes = new Class[1];
            paramTypes[0] = arguments.getClass();
            param = new Object[1];
            param[0] = arguments;
        }
        //通过反射执行Catalina类中的load方法
        Method method = catalinaDaemon.getClass().getMethod(methodName, paramTypes);
        method.invoke(catalinaDaemon, param);
    }

    public void start() throws Exception {
        if (catalinaDaemon == null) {
            init();
        }

        Method method = catalinaDaemon.getClass().getMethod("start", (Class [])null);
        method.invoke(catalinaDaemon, (Object [])null);
    }
    
    private Object getServer() throws Exception {

        String methodName = "getServer";
        Method method = catalinaDaemon.getClass().getMethod(methodName);
        return method.invoke(catalinaDaemon);
    }
    
    
    public void init() throws Exception {
        //commonLoader：加载tomcat下的lib目录的jar；
        //catalinaLoader：用于加载Catalina，Server以及其他组件，父加载器为commonloader；
        //sharedLoader：是WebappLoader web应用类加载的父类，父加载器为commonloader。
        
        //设置初始化类加载器
        initClassLoaders();
        
        //设置当前线程的类加载器为catalinaLoader
        //当前线程：初始化catalina的线程，初始化servlet容器的线程
        //Thread.currentThread().setContextClassLoader(catalinaLoader);
        
        //寻找catalina类，通过反射实例化
        Class<?> startupClass = this.getClass().getClassLoader().loadClass("iog.hytool.tomcat.startup.Catalina");
        Object startupInstance = startupClass.getConstructor().newInstance();
        
        catalinaDaemon = startupInstance;
    }

    private void initClassLoaders() {
        //初始化相关类加载器
    }
        
        
}
